const { HabitUrl, ClockInUrl, ClockInLogUrl } = require("../../../config/config")

// pages/habit/clockin/clockin.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    notLogin:false,

    /**
     * copy area
     */
    clockIcon:'dagou',
    clockWeek:["一","二","三","四","五","六","日"],
    weeks:[],
    hasClock:[],
    hasClock1:[
      "dagou",
      "dagou1",
      "dagou1",
      "dagou",
      "dagou1",
      "dagou",
      "dagou1"
    ],
    habitId:'',
    currentHabit:[],
    clockinRecord:{
      hid:'',
      uid:'',
    },
    hasLogToday:'',
    logs:[],
    navTitle:''
  },
  /*copy area*/
   /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      habitId : options.hid
    })
    let _this = this
    if(options.hid){
      this.setData({
       'clockinRecord.hid' : options.hid,
       'clockinRecord.uid' : wx.getStorageSync('userinfo').id
      })
      wx.request({
        url: HabitUrl+'/'+options.hid,
        method: 'GET',
        success: function(res){
          _this.setData({
            navTitle : res.data.result.name
          })
          wx.setNavigationBarTitle({
            title: _this.data.navTitle,
          })
          if(res.data.result.clockToday === true){
            _this.setData({
              clockIcon : 'dagou1'
            })
          }
        }
      })
      wx.request({
        url: ClockInLogUrl+'/toadyLog/'+options.hid,
        method: 'GET',
        success: function(res){
            _this.setData({
              hasLogToday : res.data.result
            })
        }
      })

      this.listWeek()
      this.listLog()

    }
  },
  clockIn:function(){
    let _this = this
    if(this.data.clockIcon === 'dagou'){
      // this.data.clockinRecord['clockinTime'] = new Date
      //1. 增加打卡记录
      wx.request({
        url: ClockInUrl,
        data: _this.data.clockinRecord,
        method: 'POST',
        //2. 改变周显示
        success: function(res){
          _this.listWeek()
        }
      })
      //3. 改变clockToday属性

      let totalDays = _this.data.currentHabit.totalDays + 1
      let currentContinue = _this.data.currentHabit.currentContinue + 1
      let mostContinue = _this.data.currentHabit.mostContinue
      if(currentContinue >= mostContinue){
        mostContinue = currentContinue
      }
      _this.setData({
        'currentHabit.clockToday' : true,
        'currentHabit.totalDays' :  totalDays,
        'currentHabit.currentContinue' : currentContinue,
        'currentHabit.mostContinue' : mostContinue
      })
      wx.request({
        url: HabitUrl,
        data: _this.data.currentHabit,
        method: 'POST'
      })
      //4. 改变按钮状态
      this.setData({
        clockIcon : 'dagou1'
      })

    }else{
      let _this = this
      wx.showModal({
        title: '取消打卡',
        content: '今天的打卡日志将会被删除，您确定删除？',
        confirmColor:'#FA5151',
        confirmText:'删除',
        success (res) {
          if (res.confirm) {
              //1. 删除打卡记录
              wx.request({
                url: ClockInUrl+'/'+_this.data.habitId,
                method: 'DELETE',
                //2. 改变周显示
                success: function(res){
                  _this.listWeek()
                }
              })
              //2. 改变clockToday属性
              let totalDays = _this.data.currentHabit.totalDays - 1
              let currentContinue = _this.data.currentHabit.currentContinue
              let mostContinue = _this.data.currentHabit.mostContinue
              if(currentContinue === mostContinue || ((currentContinue === mostContinue) && mostContinue === 1)){
                currentContinue -= 1
                mostContinue -= 1
              }else{
                currentContinue -= 1
              }
              _this.setData({
                'currentHabit.clockToday' : false,
                'currentHabit.totalDays' :  totalDays,
                'currentHabit.currentContinue' : currentContinue,
                'currentHabit.mostContinue' : mostContinue
              })
              wx.request({
                url: HabitUrl,
                data: _this.data.currentHabit,
                method: 'POST'
              })
              //3. 删除今天日志
              wx.request({
                url: ClockInLogUrl + '/habit/'+_this.data.habitId,
                method: 'DELETE'
              })
              //4. 改变按钮状态
              _this.setData({
                clockIcon : 'dagou',
                hasLogToday:false
              })
              //5. 刷新周显示与日志显示
              _this.listLog()

          } else if (res.cancel) {
            console.log('用户点击取消')
          }
        }
      })
    }

  },
  toStatistics:function(){
     wx.navigateTo({
      url: '/pages/habit/statistics/statistics?hid='+this.data.habitId,
    })
  },
  toLogDetial:function(e){
    let logId = e.currentTarget.dataset.logid
    wx.navigateTo({
      url: '/pages/habit/logDetail/logDetail?logId='+logId,
    })
  },
  listWeek:function(){
    let _this = this
    wx.request({
      url: ClockInUrl+'/list_week/'+_this.data.habitId,
      method: 'GET',
      success: function(res){
        _this.setData({
          weeks : res.data.result　
        })
      }
    })
  },
  listLog:function(){
    let _this = this
    wx.request({
      url: ClockInLogUrl+'/'+_this.data.habitId,
      method: 'GET',
      success: function(res){
        _this.setData({
          logs : res.data.result　
        })
      }
    })
  },

  toRecordLog:function(){
    wx.redirectTo({
      url: '/pages/habit/clockinLog/clockinLog?hid='+this.data.habitId,
    })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    let _this = this
    wx.request({
      url: HabitUrl+'/'+_this.data.habitId,
      method: 'GET',
      success: function(res){
        _this.setData({
          currentHabit : res.data.result
        })
      }
    })
    this.listLog()
  },
})
